<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>回调函数</title>
    <script>
      /*
        将函数作为参数传递，即为回调函数
        */
      const personArr = [
        { name: "白骨精", age: 12 },
        { name: "孙悟空", age: 20 },
        { name: "红孩儿", age: 9 },
      ];

      //若想修改if (i.age > 18)的条件，则可把该函数替换为传进参数函数
      function filter(arr, fn) {
        const newArr = [];
        for (let i of arr) {
          if (fn(i)) {
            newArr.push(i);
          }
        }
        return newArr;
      }

      //return的条件里写什么判断语句都可
      function fn(i) {
        return i.age <= 18;
      }

      console.log(filter(personArr, fn));
      console.log(personArr);

      /*
      大多传递匿名函数，筛选偶数
      */
     const arr = [1,2,3,4,5,6,7,8,9]
     let result = filter(arr,i=>i%2===0)

     console.log(result)
    </script>
  </head>
  <body></body>
</html>